package cjlu.ea.h.common;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Slf4j
public class FileUtil {

    public static void download(String filename, HttpServletResponse res) throws IOException {
        // 发送给客户端的数据
        OutputStream outputStream = res.getOutputStream();
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        // 读取filename
        String basePath = ResourceUtils.getURL("classpath:").getPath() + "static/upload/"+filename;
        bis = new BufferedInputStream(new FileInputStream(new File(basePath)));
        log.info("从:"+ basePath+"下载文件...");
        int i = bis.read(buff);
        while (i != -1) {
            outputStream.write(buff, 0, buff.length);
            outputStream.flush();
            i = bis.read(buff);
        }
        outputStream.flush();
        outputStream.close();
    }

}
